package com.ui.base;


import com.gargoylesoftware.htmlunit.javascript.host.canvas.ext.WEBGL_debug_renderer_info;
import com.ui.pojo.UIElement;
import com.ui.util.UILibraryUtil;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;



public class Base {

    private Logger logger = Logger.getLogger(Base.class);
    //共享数据
    public static WebDriver driver;

    @BeforeSuite
    @Parameters(value = {"browserType", "driverPath"})
    public void init(String browserType, String driverPath) {
        logger.info("配置信息：浏览器类型:【"+browserType+"】，驱动文件路径：【"+driverPath+"】");
        if ("ie".equalsIgnoreCase(browserType)) {
            System.setProperty("webdriver.ie.driver", driverPath);
            //创建DesirdCapabilitys对象，在对象中保存浏览器设置信息
            DesiredCapabilities capabilities = new DesiredCapabilities();
            //忽略浏览器的安全设置
            //capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS);
            //忽略浏览器的缩放设置
            capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
            //创建驱动对象，打开IE浏览器
            driver = new InternetExplorerDriver(capabilities);
            logger.info("***打开ie浏览器********");
        } else if ("firefox".equalsIgnoreCase(browserType)) {
            //设置系统变量，指定驱动文件路径，保证在创建驱动之前设置好
            System.setProperty("webdriver.gecko.driver", driverPath);
            //打开火狐浏览器
            driver = new FirefoxDriver();
            logger.info("****打开firefox浏览器****");
        } else if ("chrome".equalsIgnoreCase(browserType)) {
            //设置系统变量，指定驱动文件路径，保证在创建驱动之前设置好
            System.setProperty("webdriver.chrome.driver", driverPath);
            //打开火狐浏览器
            driver = new ChromeDriver();
            logger.info("*****打开chrome浏览器***");
        } else {
            System.out.println("输入有误，请重新输入");
        }

        driver.manage().window().maximize();
        logger.info("窗口最大化");

    }

    @AfterSuite
    public void tearDown() throws InterruptedException {
        Thread.sleep(2000);
        logger.info("*******测试完成，关闭驱动对象*********");
        driver.quit();
    }

    //显示等待
    public WebElement waitElement(By locator) {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        try {
            WebElement webElement = wait.until(ExpectedConditions.presenceOfElementLocated(locator));
            logger.info("元素定位成功");
            return webElement;
        } catch (Exception e) {
            logger.error("元素定位失败");
        }
        return null;
    }

    //根据页面元素+元素关键字来获取元素
    public WebElement getElement(String pageKeyword, String elementKeyword) {
        //根据页面关键字和元素关键字拿到ui库中的uiElement对象
        UIElement uiElement = UILibraryUtil.getUIElement(pageKeyword, elementKeyword);
        //通过拿到的UIElement对象，取出by和value属性值来判断通过什么方式来定位页面元素
        String by = uiElement.getBy();
        String value = uiElement.getValue();
        logger.info("根据{by:"+by+",value:"+value+"}来定位【"+pageKeyword+"】页面的【"+elementKeyword+"】元素");
        By locator = null;
        //通过什么选择器来定位元素，取决于配的by是什么
        if ("id".equals(by)) {
            locator = By.id(value);
        } else if ("name".equals(by)) {
            locator = By.name(value);
        } else if ("xpath".equals(by)) {
            locator = By.xpath(value);
        } else if ("className".equals(by)) {
            locator = By.className(value);
        } else if ("linkText".equals(by)) {
            locator = By.linkText(value);
        } else if ("tagName".equals(by)) {
            locator = By.tagName(value);
        } else if ("cssSelector".equals(by)) {
            locator = By.cssSelector(value);
        }
        return waitElement(locator);
    }


    //封装显示等待，出现想要的url才会停止
    public boolean urlPresenceContent(String part) {
        WebDriverWait wait = new WebDriverWait(driver, 30);
        try {
            boolean flag = wait.until(ExpectedConditions.urlContains(part));
            return flag;
        } catch (Exception e) {
            System.out.println("超时了");
        }

        return false;


    }

    /*
    * 访问测试页面*/
    public void to(String url){
        logger.info("访问测试页面：【"+url+"】");
        driver.navigate().to(url);

    }

    //往输入框写入数据
    public void sendKeys(WebElement element,String value){
        logger.info("向【"+element+"】中写入数据：【"+value+"】");
        element.sendKeys(value);

    }

    //点击元素
    public void click(WebElement element){
        logger.info("点击元素：【"+element+"】");
        element.click();

    }
    //获取元素文本值
    public String getText(WebElement element){
        String value = element.getText();
        logger.info("获取【"+element+"】元素文本值:【"+value+"】");
        return value;

    }
}


